package com.tiger.cloud.controller.crm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.model.crm.LoanInfo;
import com.tiger.cloud.service.crm.ILoanInfoService;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

/**
 * 借款信息表                                               
 */
@Controller
@RequestMapping(value = "loanInfo")
public class LoanInfoController extends BaseController {
	
	@Autowired
	private ILoanInfoService loanInfoService;
	
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public Response find(@CurrentUserId Long userId,@RequestBody LoanInfo loanInfo){
		loanInfo.setUserId(userId);
		Page<LoanInfo> datas=loanInfoService.page(loanInfo);
		return ResponseUtil.ok(datas);
	}
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public Response list(@CurrentUserId Long userId,@RequestBody LoanInfo loanInfo){
		loanInfo.setUserId(userId);
		return ResponseUtil.ok(loanInfoService.find(loanInfo));
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public Response update(@CurrentUserId Long userId,@RequestBody LoanInfo loanInfo){
		if(loanInfo.getId()!=null&&loanInfo.getId()>0){
			LoanInfo updateEntity=loanInfoService.get(loanInfo.getId());
			if(updateEntity==null||updateEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			loanInfo.setUserId(userId);
			if(loanInfoService.update(loanInfo)>0){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public Response add(@CurrentUserId Long userId,LoanInfo loanInfo){
		loanInfo.setUserId(userId);
		loanInfoService.insert(loanInfo);
		return ResponseUtil.ok();
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public Response delete(@CurrentUserId Long userId,@RequestBody LoanInfo loanInfo){
		if(loanInfo.getId()!=null&&loanInfo.getId()>0){
			LoanInfo deleteEntity=loanInfoService.get(loanInfo.getId());
			if(deleteEntity==null||deleteEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			loanInfo.setUserId(userId);
			loanInfo.setIsDelete(DeleteEnum.DELETE);
			if(loanInfoService.update(loanInfo)>0){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}

}